package framework.dubbo.demo.consumer;

import com.alibaba.dubbo.common.bytecode.ClassGenerator;
import javassist.ClassPool;
import javassist.CtClass;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.DI.IndexDao;

/**
 * DUBBO 消费者：-Dsun.misc.ProxyGenerator.saveGeneratedFiles=true
 *
 * @author ME
 * @date   2020/2/24
 */
public class DubboConsumer {

    public static void main( String[] args ) throws Exception {
        // 加载xml配置文件启动
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("framework/dubbo/demo/consumer/consumer.xml");
        context.start();
        IndexDao dao = (IndexDao) context.getBean("indexDaoImpl1");
        dao.test();
        System.out.println("[RPC] dao.test()");
        // saveClassFile(dao.getClass().getClassLoader(), "com.alibaba.dubbo.common.bytecode.proxyA0", "proxyA0");
        // saveClassFile(dao.getClass().getClassLoader(), "com.alibaba.dubbo.common.bytecode.Proxy0", "Proxy0");

        // JdkDynamicProxyUtil.createProxyClassFile("IndexDaoProxy", IndexDao.class, EchoService.class);
        System.in.read(); // 按任意键退出
    }

    public static void saveClassFile(ClassLoader loader, String className, String fileName) throws Exception {
        // File file = new File(fileName + ".class");
        // FileOutputStream out = new FileOutputStream(file);

        ClassPool pool = ClassGenerator.getClassPool(loader);
        CtClass ctClass = pool.get(className);
        ctClass.debugWriteFile();
        // out.write(ctClass.toBytecode());
        // out.close();
    }

}
